<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Edit],
  components: {
    SelectImageCheckbox: async () =>
      Vue.extend(
        await Vue.prototype.$vFN.Components.Base.SelectImageCheckbox()
      ),
  },
  data() {
    return {
      appWindow: window,
      form: {
        icon: "",
        nickName: "",
        realName: "",
      },

      imgList: [],

      apiUrl: {
        get: "mall-site-portal/portal-login/info",
        update: "mall-site-portal/portal-login/updateAdmin",
      },

      boxVisible: false,

      isLoading: false,

      isDisabled: false,
    };
  },
  watch: {
    imgList(newVal, oldVal) {
      console.log(newVal);
      this.form.icon = newVal[0];
    },
  },
  methods: {
    // 初始化
    async init() {
      this.getData();
    },

    async getData() {
      this.isLoading = true;
      let res = await this.$vFN.Http.post(
        {
          url: this.apiUrl.get,
        },
        window
      );

      this.isLoading = false;
      if (res._ok) {
        // 判断
        if (!(res.data.type == "portal")) {
          this.isDisabled = true;
        }

        for (const key in this.form) {
          this.form[key] = res.data[key];
        }

        console.log(this.form);
        this.imgList.push(this.form.icon);
      }
    },

    async submit() {
      if (!(await this.validate())) {
        return;
      }

      this.isLoading = true;
      let res = await this.$vFN.Http.post(
        {
          url: this.apiUrl.update,
          data: {
            ...this.form,
          },
        },
        window
      );

      this.isLoading = false;
      if (res._ok) {
        this.$vFN.Tools.messageSuccess(res.message);
        this.boxVisible = false;
        this.onSuccess("success");
      }
    },

    validate() {
      let that = this;
      return new Promise((resolve, reject) => {
        that.$refs["editForm"].validate((valid) => {
          if (valid) {
            resolve(true);
          } else {
            resolve(false);
          }
        });
      });
    },

    // 成功回调
    onSuccess() {},
    // 取消回调
    onCancel() {},
    // 失败回调
    onError() {},
    // 关闭窗口
    editBoxClose() {
      this.boxVisible = false;
      this.onCancel("cancel");
    },
    // 取消
    editBoxCancel() {
      this.boxVisible = false;
      this.onCancel("cancel");
    },
  },
  mounted() {
    this.boxVisible = true;
    this.init();
  },
  activated() {},
};
</script>
